/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.john.utils;

import com.john.Pert;
import com.john.Task;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author John
 */
public class CreatePertFile {

    public void printDotFile(List<Task> taska, Pert pert, LongestPath lon) {

        String fileLocation = "D:\\John\\graph.dot";
        File file = new File("graph.dot");
        System.out.println("->"+file.getAbsolutePath());
          JOptionPane.showMessageDialog(null, "DOtFile Location " + file.getAbsolutePath());
        PrintStream out = null;
        //ArrayList<Task> ff=new ArrayList();
        try {
            out = new PrintStream(new FileOutputStream(file));
            out.print("digraph myPERT {");
            for (Task task : taska) {
                String format = task.gettIdentifier() + "[shape=tripleoctagon, sides=5, style=\"bold\", color=\"black\",label=\"" + task.gettIdentifier() + "(" + task.getDuration() + " days)\"]";
                out.println(format);

            }

            for (Iterator<com.john.Edge> it = pert.getEdges().iterator(); it.hasNext();) {
                com.john.Edge e = it.next();
                String is = e.isIsCritical() ? "red" : "black";
                System.out.println("is=" + is);
                String line = ("" + e.getSource().gettIdentifier() + "->" + e.getDestination().gettIdentifier() + "[label=\"\", style=\"bold\", color=\"" + is + "\"]  ");
                out.println(line);
            }

            out.println("}");
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "ERROR: " + e.getMessage());
            // TODO: handle exception
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
